//
//  DetailSuperViewController.m
//  uplife
//
//  Created by ZhangCheng on 2018/12/26.
//  Copyright © 2018年 ZhangCheng. All rights reserved.
//

#import "DetailSuperViewController.h"

#import "DetailViewController.h"
#import "DetailForBuildTableViewController.h"
#import "GongYuListViewController.h"
#import "ShouYiViewController.h"
#import "ZuYueViewController.h"
#import "FilesViewController.h"
#import "FileListViewController.h"
@interface DetailSuperViewController ()<UIScrollViewDelegate>

@property(nonatomic,weak)IBOutlet UISegmentedControl *segmentControl;
@property(nonatomic,weak)IBOutlet UIView *pickerViewSuperView;
//@property(nonatomic,strong)NSArray *yearsArray;
@end

@implementation DetailSuperViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.realtyId=@"";
    
    
    [self sendRequestForLoadData];
}



-(void)showPickerView{
    self.pickerViewSuperView.alpha=0.0f;
    self.pickerViewSuperView.hidden = NO;
    [UIView animateWithDuration:0.25f animations:^{
        self.pickerViewSuperView.alpha=1.0f;
    }];
}

-(IBAction)closePickerView{
    [UIView animateWithDuration:0.25f animations:^{
        self.pickerViewSuperView.alpha=0.0f;
    } completion:^(BOOL finished) {
        self.pickerViewSuperView.hidden = YES;
        if (self.delegate && [self.delegate respondsToSelector:@selector(detailViewPickerViewDidClose)]) {
            [self.delegate detailViewPickerViewDidClose];
        }
    }];
}

-(void)sendRequestForLoadData{
    NSString *urlString = [NSString stringWithFormat:@"%@/realty/get-one.do?id=%@&moneyTypeCode=%@&type=1",JSXF_HOST,self.dataId,[ZCTool sharedInstance].currentSelectedHuoBiDic[@"code"]];
    
    [SVProgressHUD show];
    
    [ZCWebServiceHelper requestWithMethod:GET andUrlStr:urlString andParameters:nil andFinished:^(id response, NSURLSessionDataTask *dataTask) {
        [SVProgressHUD dismiss];
        if (![response[@"data"] isKindOfClass:[NSString class]]) {
            NSDictionary *dic = response;
            self.dataDic = dic[@"data"];
            if (self.isGongYu==NO) {
                NSArray *array = self.dataDic[@"rooms"];
                for (NSDictionary *dic in array) {
                    NSString *nameString = [NSString stringWithFormat:@"%@",dic[@"roomNum"]];
                    if ([nameString isEqualToString:@"一栋"]) {
                        self.realtyId = [NSString stringWithFormat:@"%@",dic[@"realtyId"]];
                        break;
                    }
                }
            }
            
            
            [self setupChildrenViewController];
        }
        
    }];
}

-(void)setupChildrenViewController{
    
    UINavigationController *nav1;
    if (self.isGongYu) {
        DetailViewController *vc1 = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"detailViewController"];
        vc1.fatherViewController = self;
        nav1 = [[UINavigationController alloc]initWithRootViewController:vc1];
    }else{
        DetailForBuildTableViewController *vc1 = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"detailViewControllerForBuild"];
        vc1.fatherViewController = self;
        nav1 = [[UINavigationController alloc]initWithRootViewController:vc1];
    }
    [nav1 setNavigationBarHidden:YES];
    [self addChildViewController:nav1];
    UIView *view1 = [self.scrollView viewWithTag:1];
    [view1 addSubview:nav1.view];
    [nav1.view setFrame:CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
    
    UINavigationController *nav2;
    if (self.isGongYu) {
        ZuYueViewController *vc2 = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"zuYueViewController"];
        vc2.fatherViewController = self;
        vc2.roomId = self.dataDic[@"roomId"];
        nav2 = [[UINavigationController alloc]initWithRootViewController:vc2];
    }else{
        GongYuListViewController *vc2 = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"gongYuListViewController"];
        vc2.fatherViewController = self;
        vc2.type = 2;
        nav2 = [[UINavigationController alloc]initWithRootViewController:vc2];
    }
    [nav2 setNavigationBarHidden:YES];
    [self addChildViewController:nav2];
    UIView *view2 = [self.scrollView viewWithTag:2];
    [view2 addSubview:nav2.view];
    [nav2.view setFrame:CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
    
    UINavigationController *nav3;
    if (self.isGongYu) {
        ShouYiViewController *vc3 = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"shouYiViewController"];
        vc3.fatherViewController = self;
        vc3.roomId = self.dataDic[@"roomId"];
        nav3 = [[UINavigationController alloc]initWithRootViewController:vc3];
        
    }else{
        ShouYiViewController *vc3 = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"shouYiViewControllerForBuild"];
        vc3.fatherViewController = self;
        nav3 = [[UINavigationController alloc]initWithRootViewController:vc3];
        
    }
    
    [nav3 setNavigationBarHidden:YES];
    [self addChildViewController:nav3];
    UIView *view3 = [self.scrollView viewWithTag:3];
    [view3 addSubview:nav3.view];
    [nav3.view setFrame:CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
    
    UINavigationController *nav4;
    if (self.isGongYu) {
        FilesViewController *vc4 = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"filesViewController"];
        vc4.fatherViewController = self;
        vc4.roomId = self.dataDic[@"roomId"];
        nav4 = [[UINavigationController alloc]initWithRootViewController:vc4];
    }else{
        GongYuListViewController *vc4 = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"gongYuListViewController"];
        vc4.fatherViewController = self;
        vc4.type = 4;
        nav4 = [[UINavigationController alloc]initWithRootViewController:vc4];
    }
    [nav4 setNavigationBarHidden:YES];
    [self addChildViewController:nav4];
    UIView *view4 = [self.scrollView viewWithTag:4];
    [view4 addSubview:nav4.view];
    [nav4.view setFrame:CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
}
    

-(IBAction)segmentAction:(UISegmentedControl*)sender{
    NSLog(@"action:%@",@(sender.selectedSegmentIndex));
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    UINavigationController *nav = self.childViewControllers[sender.selectedSegmentIndex];
    [nav popToRootViewControllerAnimated:NO];
    [self.scrollView setContentOffset:CGPointMake(sender.selectedSegmentIndex * screenWidth, 0)];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    CGFloat x = scrollView.contentOffset.x;
    [self.segmentControl setSelectedSegmentIndex:(x/screenWidth + 0.5f)];
}

-(void)pushFileListVC:(NSString *)keyString{
    [self performSegueWithIdentifier:@"toFileListVC" sender:nil];
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    if ([segue.identifier isEqualToString:@"toFilesListViewController"]) {
        FileListViewController *vc = segue.destinationViewController;
        vc.dataDic = sender;
    }
}


@end
